home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’87 / Source ƒ.sit / Source ƒ / C ƒ / TRANS-LSC / FakeAlert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-12  |  3.8 KB  |  163 lines  |  [TEXT/KAHL]

  1. # include    <DialogMgr.h>
  2.  
  3.  
  4. # define    nil        (0L)
  5.  
  6.  
  7. /*
  8.     In-memory item list for dialog with four items:
  9.  
  10.     1    "^0^1^2^3" (static text)
  11.     2    Button 1
  12.     3    Button 2
  13.     4    Button 3
  14.  
  15.     The caller of FakeAlert passes the four strings that are to be
  16.     substituted into the first item, the number of buttons that
  17.     should be used, and the titles to put into each button.
  18.     A copy of the item list is hacked to use the right number of
  19.     buttons.
  20.  
  21.     Thanks to Erik Kilk and Jason Haines.  Some of the stuff to do
  22.     this is modified from code they wrote.
  23. */
  24.  
  25.  
  26. static int    itemList [] =
  27. {
  28.     3,                    /* max number of items - 1 */
  29.  
  30. /*
  31.     statText item
  32. */
  33.     0, 0,                /* reserve a long for item handle */
  34.     10, 27, 61, 225,    /* display rectangle */
  35.     ((8+128) << 8) | 8,    /* 8 + 128 = statText (disabled), title 8 bytes long */
  36.     '^0', '^1',        /* ^0^1^2^3 */
  37.     '^2', '^3',
  38.  
  39. /*
  40.     first button
  41. */
  42.  
  43.     0, 0,                /* reserve a long for item handle */
  44.     104, 140, 124, 210,    /* display rectangle */
  45.     (4 << 8) | 0,        /* 4 = pushButton, title is 0 bytes long*/
  46.  
  47. /*
  48.     second button
  49. */
  50.  
  51.     0, 0,                /* reserve a long for item handle */
  52.     104, 30, 124, 100,    /* display rectangle */
  53.     (4 << 8) | 0,        /* 4 = pushButton, title is 0 bytes long */
  54.  
  55. /*
  56.     third button
  57. */
  58.  
  59.     0, 0,                /* reserve a long for item handle */
  60.     72, 30, 92, 100,    /* display rectangle */
  61.     (4 << 8) | 0        /* 4 = pushButton, title is 0 bytes long */
  62. };
  63.  
  64.  
  65. /*
  66.     Set dialog button title and draw bold outline if makeBold true.
  67.     This must be done after the window is shown or else the bold
  68.     outline won't show up (which is probably the wrong way to do it).
  69. */
  70.  
  71. static SetDControl (theDialog, itemNo, title, makeBold)
  72. DialogPtr    theDialog;
  73. int            itemNo;
  74. StringPtr    title;
  75. Boolean        makeBold;
  76. {
  77. Handle        itemHandle;
  78. int            itemType;
  79. Rect        itemRect;
  80. PenState    pState;
  81.  
  82.     GetDItem (theDialog, itemNo, &itemType, &itemHandle, &itemRect);
  83.     SetCTitle (itemHandle, title);
  84.     if (makeBold)
  85.     {
  86.         GetPenState (&pState);
  87.         PenNormal ();
  88.         PenSize (3, 3);
  89.         InsetRect (&itemRect, -4, -4);
  90.         FrameRoundRect (&itemRect, 16, 16);
  91.         SetPenState (&pState);
  92.     }
  93. }
  94.  
  95.  
  96. /*
  97.     Fake an alert, using an in-memory window and item list.
  98.     The message to be presented is constructed from the first
  99.     four arguments.  nButtons is the number of buttons to use,
  100.     defButton is the default button, the next three args are
  101.     the titles to put into the buttons.  The return value is
  102.     the button number (1..nButtons).  This must be interpreted
  103.     by the caller, since the buttons may be given arbitrary
  104.     titles.
  105.  
  106.     nButtons should be between 1 and 3, inclusive.
  107.     defButton should be between 1 and nButtons, inclusive.
  108. */
  109.  
  110.  
  111. FakeAlert (s1, s2, s3, s4, nButtons, defButton, t1, t2, t3)
  112. StringPtr    s1, s2, s3, s4;
  113. int            nButtons;
  114. int            defButton;
  115. StringPtr    t1, t2, t3;
  116. {
  117. GrafPtr        savePort;
  118. register DialogPtr    theDialog;
  119. register Handle        iListHandle;
  120. Rect        bounds;
  121. int            itemHit;
  122.  
  123.     InitCursor ();
  124.     GetPort (&savePort);
  125.     iListHandle = NewHandle (512L);
  126.     HLock (iListHandle);
  127.     BlockMove (&itemList, *iListHandle, 512L);
  128.     ((int *) *iListHandle)[0] = nButtons;    /* = number items - 1 */
  129.     SetRect (&bounds, 115, 80, 355, 220);
  130.     theDialog = NewDialog (nil, &bounds, "\p", false, dBoxProc, -1L,
  131.                             false, 0L, iListHandle);
  132.  
  133.     ParamText (s1, s2, s3, s4);        /* construct message */
  134.  
  135.     SetPort (theDialog);
  136.     ShowWindow (theDialog);
  137.  
  138.     switch (nButtons)                /* set button titles */
  139.     {
  140.         case 3:
  141.             SetDControl (theDialog, 4, t3, defButton == 3);
  142.             /* fall through... */
  143.         case 2:
  144.             SetDControl (theDialog, 3, t2, defButton == 2);
  145.             /* fall through... */
  146.         case 1:
  147.             SetDControl (theDialog, 2, t1, defButton == 1);
  148.     }
  149.  
  150. /*
  151.     ModalDialog returns 1 if return/enter hit, which, since
  152.     the statText item is first, can be unambiguously
  153.     interpreted as "choose default".
  154. */
  155.     ModalDialog (nil, &itemHit);
  156.     itemHit = (itemHit == 1 ? defButton : itemHit - 1);
  157.     HUnlock (iListHandle);
  158.     /*HPurge (iListHandle);*/
  159.     DisposDialog (theDialog);
  160.     SetPort (savePort);
  161.     return (itemHit);
  162. }
  163.